<?php


namespace app\controller\api;


use app\base\BaseEncryptController;
use app\model\content\ContentArticleModel;
use app\model\content\ContentTypeModel;
use think\Request;

class Article extends BaseEncryptController
{
    protected function initialize()
    {
        parent::initialize();
    }

    /**
     * 获取文章列表
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getNavList()
    {
        $content_type_model = new ContentTypeModel();

        $map = [
            'status' => 1
        ];
        $list = $content_type_model->getList($map, 'sort DESC', 'id,type,name');
        return self::returnMsg(1, $list);
    }

    /**
     * 获取文章列表
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArticleList(Request $request)
    {
        $type_id = $request->param('type_id');
        $page = $request->param('page');
        $limit = $request->param('limit');

        $content_article_model = new ContentArticleModel();
        if (!empty($type_id)) {
            $map = [
                'type_id' => $type_id,
                'status' => 1
            ];
        } else {
            $map = [
                'status' => 1
            ];
        }

        $data_list = $content_article_model->getPageData($map, '', $page, $limit);
        $items = $data_list['dataList'];
        foreach ($items as &$vaule) {
            ContentArticleModel::formatx($vaule);
        }
        return self::returnMsg(1, $items);
    }

    /**
     * 获取文章详情
     * @param string $id
     * @return \think\response\Json
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     */
    public function getArticleInfo($id = '')
    {
        $content_article_model = new ContentArticleModel();
        $data = $content_article_model->getInfo($id);
        ContentArticleModel::formatx($data);
        return self::returnMsg(1, $data);
    }
}